package com.xcity.game.combat.skill.impl;

import java.util.List;

import com.xcity.game.combat.Action;
import com.xcity.game.combat.ActionEffect;
import com.xcity.game.combat.CombatUtils;
import com.xcity.game.combat.Round;
import com.xcity.game.combat.attr.CombatAttributeType;
import com.xcity.game.combat.buff.CombatBuff;
import com.xcity.game.combat.skill.CombatSkill;
import com.xcity.game.combat.unit.CombatUnit;
import com.xcity.game.common.GameStaticConfig;
import com.xcity.game.skill.Skill;

/**
 * 雨露均沾<br/>
 * 进行一次群体攻击对所有目标造成{0}%的技能伤害，每命中一个目标提升己方全体员工攻击力{1}%
 * @author yang.li
 *
 */
public class COMBAT_SKILL_90119 extends CombatSkill {

	public COMBAT_SKILL_90119(Skill skill) {
		super(skill);
	}

	@Override
	public void calcDamage(Action action) {
		CombatUnit source = action.unit;
		List<CombatUnit> targets = action.target.all();
		float rate = skill.getParameter(0) / GameStaticConfig.percentBase;
		float addAttackPowerBaseRate = skill.getParameter(1) / GameStaticConfig.percentBase;
		float addAttackPowerRate = 0f;
		for (int i = 0; i < targets.size(); i++) {
			CombatUnit target = targets.get(i);
			if (!target.isDied()) {
				ActionEffect eff = action.addTarget(target);
				CombatUtils.calcMagicAttack(eff, rate);
				if (eff.isHit()) {
					addAttackPowerRate += addAttackPowerBaseRate;
				}
			}
		}
		if (addAttackPowerRate > 0f) {
			List<CombatUnit> units = source.getTeam().all();
			for (int i = 0; i < units.size(); i++) {
				CombatUnit unit = units.get(i);
				if (!unit.isDied()) {
					action.addBuff(new BUFF(skill, unit, rate));
				}
			}
		}
	}
	
	// 加攻
	static class BUFF extends CombatBuff {
		float preAttackPower;
		public BUFF(Skill skill, CombatUnit owner, float rate) {
			super(skill.getTemplate().getCombatBuff(0), owner);
			float attackPower = owner.getAttackPower();
			preAttackPower = attackPower;
			attackPower += attackPower * rate;
			owner.setAttributeValue(CombatAttributeType.ATTACK_POWER, attackPower);
		}
		@Override
		public void afterAction(Action action) {
			roundEnd(action.ref());
		}
		@Override
		public void roundEnd(Round round) {
			super.roundEnd(round);
			if (state == STATE_ADDED) {
//				uneffect();
//				round.addBuff(this);
				owner.setAttributeValue(CombatAttributeType.ATTACK_POWER, preAttackPower);
			}
		}
	}

}
